﻿
var active_nav = function (active)
{
	if (active && active.length) {
		var that = active.parent('ul');
		if (!that.hasClass("nav-pills")) {
			that.addClass("in");
			that.prev().addClass("active");
			return that;
		}
	}

	return false;
}

// 指定焦点
function activeing(key,f, isActive)
{
	var active = $("" + f + " [data-key=" + key + "]").eq(0);
	var foractiveing = function (_that)
	{
		__that = active_nav(_that);
		if (__that) {
			foractiveing(__that.parent('li'));
		}
	}

	if (isActive) {
		active.addClass("active");
	}
	
	active = foractiveing(active);
}

if (window.view) {
	activeing(window.view, "#mycollapse", true);
}


$(function () {

	var nav_a = $("#mycollapse a");
	var search_ul = $("#search_ul");

	$(".mc-search").find('input').on('input propertychange', function () {

		var result = $(this).val();

		if (!result) {
			return search_ul.hide();
		}

		result = result.replace(/level/g,"");
		result = result.replace(/#/g, "");
		result = result.replace(/data/g, "");
		result = result.replace(/href/g, "");
		result = result.replace(/class/g, "");
		result = result.replace(/id/g, "");
		result = result.replace(/active/g, "");
		result = result.replace(/second/g, "");
		result = result.replace(/accordion/g, "");
		result = result.replace(/toggle/g, "");
		result = result.replace(/nav/g, "");
		result = result.replace(/pills/g, "");
		result = result.replace(/stacked/g, "");
		result = result.replace(/border/g, "");
		result = result.replace(/bottom/g, "");
		result = result.replace(/margin/g, "");
		result = result.replace(/22/g, "");
		result = result.replace(/span/g, "");
		result = result.replace(/div/g, "");
		result = result.replace(/li/g, "");
		result = result.replace(/ul/g, "");

		if (result.length > 1) {
			result = result.toLowerCase().replace(/(^\s*)|(\s*$)/g, "");

			// 最终结果
			var list = [];
			// 匹配树据
			var arr = result.split(" ");
			// 有效匹配树据
			var resultArr = [];


			if (arr.length === 1) {
				arr = result.split(">");
			}

			for (var i in arr) {
				if (arr[i].length > 1) {
					resultArr.push(arr[i]);
				}
			}

			$("#mycollapse > ul > li").each(function () {

				var _this = $(this);

				var aHtml = _this.html();

				for (var i in resultArr) {
					if (aHtml.indexOf(resultArr[i]) > -1) {

						console.log(resultArr)

						list.push("<li data-key='" + _this.attr("data-key") + "'>" + aHtml + "</li>");
					}
				}

			});


			if (list.length) {

				search_ul.show();

				var html = list.join(" ");

				for (var i in resultArr) {
					if (html.indexOf(resultArr[i]) > -1) {
						html = html.replace(new RegExp(resultArr[i], "g"), "<span class='nav_select' style='font-weight: bold;' >" + resultArr[i] + "</span>");
					}
				}

				html = html.replace(/id="level/g, 'id="level-c').replace(/href="#level/g, 'href="#level-c');
				html = html.replace(new RegExp("height: 0px;", "g"), "");
				html = html.replace(new RegExp("active", "g"), "");
				html = html.replace(new RegExp("in", "g"), "");
				search_ul.html(html);

				$("#search_ul .nav_select").each(function () {
					var ppp = $(this).parent().parent();
					var datakey = ppp.attr("data-key");
					if (!datakey) {
						datakey = ppp.parent().attr("data-key");
					}
					activeing(datakey, "#search_ul", false);
				});

			} else {
				search_ul.hide();
			}
		}

	});

	//$(".mc-search111").find('input').on('input propertychange', function () {

	//	var result = $(this).val();

	//	if (!result) {
	//		return search_ul.hide();
	//	}

	//	if (result.length > 1) {

	//		var list = [];

	//		nav_a.each(function () {

	//			var _this = $(this);

	//			var aHtml = _this.html();

	//			if (aHtml.indexOf(result) > -1 && !_this.hasClass("accordion-toggle")) {
	//				list.push("<a href='" + _this.attr("href") + "'>" + aHtml + "</a>");
	//			}

	//		});


	//		if (list.length) {
	//			search_ul.show();
	//			search_ul.html("<li>" + list.join(" ") + "</li>");
	//		} else {
	//			search_ul.hide();
	//		}
	//	}

	//});

});




$(document).on('click', '.accordion-toggle', function (event) {

	event.stopPropagation();
	var $this = $(this).eq(0);

	var _active_nav = function (active) {
		if (active) {
			var that = active.parent('ul');
			if (!that.hasClass("nav-pills")) {
				return that;
			}
		}
		return false;
	}

	var _gettopli = function (_that) {
		__that = _active_nav(_that);
		if (__that) {
			_gettopli(__that.parent('li'));
		}
		return _that;
	}

	var that = _gettopli($this.parent('li'));

	// 获取兄弟节点
	var actives = that.siblings();

	if (actives && actives.length) {
		actives.find('.in').data('collapse');
		actives.find('.in').collapse('hide');
	}

	if ($this.hasClass("active")) {
		$this.removeClass("active");
	} else {
		actives.find('a').removeClass("active");
		$this.addClass("active");
	}

	var target = $this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, ''); //strip for ie7
	$(target).collapse('toggle');
});